package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SSeckillMapper;
import com.yuelife.service.pojo.SSeckill;
import com.yuelife.service.service.SSeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SSeckill业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SSeckillServiceImpl implements SSeckillService {

    @Autowired
    private SSeckillMapper sSeckillMapper;


    /**
     * SSeckill条件+分页查询
     * @param sSeckill 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SSeckill> findPage(SSeckill sSeckill, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sSeckill);
        //执行搜索
        return new PageInfo<SSeckill>(sSeckillMapper.selectByExample(example));
    }

    /**
     * SSeckill分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SSeckill> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SSeckill>(sSeckillMapper.selectAll());
    }

    /**
     * SSeckill条件查询
     * @param sSeckill
     * @return
     */
    @Override
    public List<SSeckill> findList(SSeckill sSeckill){
        //构建查询条件
        Example example = createExample(sSeckill);
        //根据构建的条件查询数据
        return sSeckillMapper.selectByExample(example);
    }


    /**
     * SSeckill构建查询对象
     * @param sSeckill
     * @return
     */
    public Example createExample(SSeckill sSeckill){
        Example example=new Example(SSeckill.class);
        Example.Criteria criteria = example.createCriteria();
        if(sSeckill!=null){
            // 秒杀活动id
            if(!StringUtils.isEmpty(sSeckill.getSsId())){
                    criteria.andEqualTo("ssId",sSeckill.getSsId());
            }
            // 商品id
            if(!StringUtils.isEmpty(sSeckill.getServiceId())){
                    criteria.andEqualTo("serviceId",sSeckill.getServiceId());
            }
            // 秒杀数量
            if(!StringUtils.isEmpty(sSeckill.getPsCount())){
                    criteria.andEqualTo("psCount",sSeckill.getPsCount());
            }
            // 秒杀开始时间
            if(!StringUtils.isEmpty(sSeckill.getStartTime())){
                    criteria.andEqualTo("startTime",sSeckill.getStartTime());
            }
            // 秒杀结束时间
            if(!StringUtils.isEmpty(sSeckill.getEndTime())){
                    criteria.andEqualTo("endTime",sSeckill.getEndTime());
            }
            // 0-未开始 1-进行中  2-已结束
            if(!StringUtils.isEmpty(sSeckill.getStatus())){
                    criteria.andEqualTo("status",sSeckill.getStatus());
            }
            // 当前价格
            if(!StringUtils.isEmpty(sSeckill.getCurrentPrice())){
                    criteria.andEqualTo("currentPrice",sSeckill.getCurrentPrice());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sSeckillMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SSeckill
     * @param sSeckill
     */
    @Override
    public void update(SSeckill sSeckill){
        sSeckillMapper.updateByPrimaryKey(sSeckill);
    }

    /**
     * 增加SSeckill
     * @param sSeckill
     */
    @Override
    public void add(SSeckill sSeckill){
        sSeckillMapper.insert(sSeckill);
    }

    /**
     * 根据ID查询SSeckill
     * @param id
     * @return
     */
    @Override
    public SSeckill findById(Integer id){
        return  sSeckillMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SSeckill全部数据
     * @return
     */
    @Override
    public List<SSeckill> findAll() {
        return sSeckillMapper.selectAll();
    }

    @Override
    public List<SSeckill> selectSSeckillByssId() {
        return sSeckillMapper.selectSSeckillByssId();
    }
}
